/*
 * @Copyright: Copyright (c) 2030 吴周海
 * @Author: wzh200x@126.com
 * @Date: 2025-10-11 15:26:44
 * @LastEditors: wzh200x@126.com
 * @LastEditTime: 2025-10-20 14:04:07
 * @FilePath: \gogamewebserver\goGameWebServer\internal\controller\auth\auth_v1.go
 * @Description: 文件功能描述
 */
package auth

import (
	"context"
	v1 "goGameWebServer/api/auth/v1"
	"goGameWebServer/internal/controller"
	"goGameWebServer/internal/defines/cerror"
	"goGameWebServer/internal/defines/entityName"
	"goGameWebServer/internal/defines/platform"
	"goGameWebServer/internal/jwt"
	"goGameWebServer/internal/manager/platformmanager"
	"goGameWebServer/internal/model/entity"
	"goGameWebServer/internal/register"

	"github.com/gogf/gf/v2/frame/g"
)

func (c *AuthControllerV1) Login(context context.Context, req *v1.AuthLoginReq) (res *v1.AuthLoginRes, err error) {
	// 查询账号信息
	ac := register.GetModel(entityName.EntityName_Account)
	acData := ac.GetEntityData().(*entity.AccountModelData)
	acData.AccountName = req.AccountName
	acData.Password = req.Password
	acData.ThirdPlatformType = req.ThirdPlatformType
	acData.ThirdPlatform = req.ThirdPlatform
	acData.ThirdAccount = req.ThirdAccount
	acData.ThirdToken = req.ThirdToken
	acData.ThirdTokenExpireTime = req.ThirdTokenExpireTime
	e := ac.LoadEntityDataFromDB(nil)
	res = &v1.AuthLoginRes{}
	if e != nil {
		controller.BackResponseError(g.RequestFromCtx(context), cerror.HTTP_ACCOUNT_NOT_FOUND_ERROR,
			cerror.GetErrMsg(cerror.HTTP_ACCOUNT_NOT_FOUND_ERROR))
	} else {
		jwtToken, tokenExpireTime, e := jwt.GenerateToken(uint(acData.Id), acData.AccountName)
		if e != nil {
			controller.BackResponseError(g.RequestFromCtx(context), -1, e.Error())
			return
		}
		res.AccountId = acData.Id
		res.AccountName = acData.AccountName
		res.AvatarId = acData.AvatarId
		res.ThirdAccount = acData.ThirdAccount
		res.AccessToken = jwtToken
		res.TokenExpireTime = tokenExpireTime
		controller.BackResponseOK(g.RequestFromCtx(context), res)
	}
	return
}

func (c *AuthControllerV1) ThirdLogin(context context.Context, req *v1.AuthPlatformLoginReq) (
	res *v1.AuthPlatformLoginRes, err error) {
	// 获取平台操作接口
	platformOpt := platformmanager.ManagerPlatform().GetPlatform(req.ThirdPlatformType)
	if req.ThirdPlatformType >= platform.PLATFORM_MAX || platformOpt == nil {
		controller.BackResponseError(g.RequestFromCtx(context), cerror.HTTP_PLATFORM_TYPE_ERROR,
			cerror.GetErrMsg(cerror.HTTP_PLATFORM_TYPE_ERROR))
		return
	}
	// 获取平台配置
	platformOption := platformmanager.ManagerPlatform().GetPlatformOption(req.AppId)
	if platformOption == nil {
		controller.BackResponseError(g.RequestFromCtx(context), cerror.HTTP_PLATFORM_APPID_ERROR,
			cerror.GetErrMsg(cerror.HTTP_PLATFORM_APPID_ERROR))
		return
	}
	// 调用平台登录接口
	re, e := platformOpt.Login(req.ThirdCode, platformOption)
	if e != nil {
		controller.BackResponseError(g.RequestFromCtx(context), cerror.HTTP_PLATFORM_LOGIN_ERROR,
			cerror.GetErrMsg(cerror.HTTP_PLATFORM_LOGIN_ERROR)+e.Error())
		return
	}
	res = &v1.AuthPlatformLoginRes{}
	res.ThirdAccount = re["open_id"].(string)
	res.ThirdSessionKey = re["session_key"].(string)
	controller.BackResponseOK(g.RequestFromCtx(context), res)
	return
}
